/**  
 * All rights Reserved, Designed By <a href="www.itttq.com" target="_blank">www.itttq.com</a>
 * @date:   2017-07-21 11:42:21
 * @Copyright: 2017 <a href="www.itttq.com" target="_blank">www.itttq.com</a> Inc. All rights reserved. 
 */
package com.itttq.gshop.pbpm.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.itttq.gshop.domain.po.PayApplyPo;
import com.itttq.gshop.domain.to.ResultPageTo;
import com.itttq.gshop.domain.to.ResultTo;
import com.itttq.gshop.generic.controller.GenericController;
import com.itttq.gshop.generic.util.Constants.PayMethod;
import com.itttq.gshop.generic.util.Constants.PayStatus;
import com.itttq.gshop.pbpm.service.PayApplyService;

@RestController
@RequestMapping("payApply")
public class PayApplyController extends GenericController {
	private static final long serialVersionUID = -4466967966900250L;
	public static final String CACHE_KEY = "payApply";
	@Autowired
	private PayApplyService payApplyService;
	
	/**
	 * 根据条件获取PayApplyPo对象，并返回查询的数据总数
	 * 
	 * @param payApply
	 * @Author 迷蝶
	 * @return ResultPageTo<PayApplyPo>
	 */
	@RequestMapping("list")
    public ResultPageTo<PayApplyPo> getPayApplys(@RequestBody PayApplyPo payApply) {
        return payApplyService.getPayApplys(payApply);
    }
	
	/**
	 * 根据ID获取PayApplyPo对象，并放入redis缓存
	 * 
	 * @param id	只能是数字
	 * @Author 迷蝶
	 * @return PayApplyPo
	 */
	@RequestMapping("{id:^[0-9]*$}")
	@Cacheable(value=CACHE_KEY, key="'payApply_'+#id")
	public PayApplyPo getPayApply(@PathVariable("id") int id) {
		return payApplyService.getPayApply(id);
	}
	
	/**
	 * 根据订单ID生成支付请求，如果是线下支付，直接为支付成功并更新订单状态
	 * 
	 * @param orderId		订单ID
	 * @param payMethodId		支付方式ID。写死id=1为线下支付
	 * @Author 迷蝶
	 * @return ResultTo
	 * @throws Exception 
	 */
	@RequestMapping("payApply/{orderId:^[0-9]*$}/{payMethodId:^[0-9]*$}")
	public ResultTo payApply(@PathVariable("orderId") int orderId, @PathVariable("payMethodId") int payMethodId) {
		ResultTo rt = null;
		try {
			rt = payApplyService.payApply(orderId, payMethodId);
		} catch (Exception e) {
			rt = new ResultTo(ResultTo.PAY_ERR);
			e.printStackTrace();
		}
		return rt;
	}
	
	/**
	 * 根据ID删除PayApplyPo对象，并清除此ID的缓存。<br>
	 * 
	 * @param id	只能是数字
	 * @Author 迷蝶
	 * @return PayApplyPo
	 */
	@RequestMapping("payApply/{id:^[0-9]*$}")
	@CacheEvict(value=CACHE_KEY, key="'payApply_'+#id")
	public Integer delPayApply(@PathVariable("id") int id) {
		return payApplyService.delPayApply(id);
	}
	
	/**
	 * 处理支付结果，只校验支付金额是否和支付一致、支付方式，不做通知的签名校验。
	 * 
	 * @param orderId
	 * @param payApplyId
	 * @param payAmount
	 * @param pm
	 * @param ps
	 * @throws Exception
	 * @Author 迷蝶
	 * @return ResultTo
	 */
	@RequestMapping("payResult/{id:^[0-9]*$}")
	public ResultTo payResult(Integer orderId, @PathVariable("id") int payApplyId, int payAmount, PayMethod pm, PayStatus ps) {
		return payApplyService.payResult(orderId, payApplyId, payAmount, pm, ps);
	}
}